<?php
/**
 * Advice.class.php
 *
 * @author 周剑锋 <camfee@foxmai.com>
 *
 */
namespace controls\MobileApi\Center;

use MobileApi\ApiBase;
use Center\UserAdvice;

/**
 * 用户反馈信息相关
 *
 * @package Center
 * @author 周剑锋 <camfee@foxmai.com>
 * @since   1.0.0 2017-01-03
 */
class Advice extends ApiBase
{

    /**
     * 新增用户反馈信息
     *
     * <pre>
     * POST
     *      contents: 反馈内容
     *      contact：联系方式(可选)
     * </pre>
     *
     * @return void|string 返回JSON数据
     *
     * <pre>
     * {
     *    "Status": 200,
     *    "Result": {}
     * }
     *
     * 异常状态
     * 201: 反馈内容不能为空！
     * 202: 提交失败，请稍后再试！
     * </pre>
     */
    public function addAdvice()
    {
        $uid = $this->isLogin(false);
        if ($uid < 1) {
            $uid = 0;
        }
        $deviceid = $GLOBALS['g_deviceid'];
        $content = trim($_POST['contents']);
        if (empty($content)) {
            $this->output(201, '反馈内容不能为空！');
        } else {
            $content = mb_substr($content, 0, 2000);
        }
        $contact = trim($_POST['contact']);
        $data = [
            'UserId' => $uid,
            'UserAgent' => $_SERVER['HTTP_USER_AGENT'] . '|' . $GLOBALS['g_ver'],
            'Contact' => $contact
        ];

        $rel = UserAdvice::addUserAdvice($deviceid, $content, $data);
        if ($rel) {
            $this->output();
        } else {
            $this->output(202, '提交失败，请稍后再试！');
        }
    }
}